<?php

class M_class extends CI_Model {

    public $temp_node = array();
    public $temp_tree = array();

    function __construct() {
        parent::__construct();
    }

    function get_count($data) {
        $this->db->from('class');
        $this->db->select('count(1) as cnt');
        $this->db->where($data);
        $result = $this->db->get()->row_array();
        return $result['cnt'];
    }

    function get_class($data) {
        if (isset($data['column'])) {
            $this->db->select($data['column']);
        }
        $this->db->from('class');
        if (!isset($data['classid'])) {
            return false;
        }
        $this->db->where('classid', $data['classid']);
        return $this->db->get()->row_array();
    }

    function get_class_in_id($id_list) {
        $list = array();
        foreach ($id_list as $k => $v) {
            $v = intval($v);
            if ($v > 1) {
                $list[] = $v;
            }
        }
        if (empty($list)) {
            return null;
        }
        $this->db->where_in('classid', $list);
        return $this->db->get('class')->result_array();
    }

    function get_all($data = array()) {
        if (isset($data['column'])) {
            $this->db->select($data['column']);
        }
        if (isset($data['where'])) {
            if(isset($data['where']['classname'])){
                $this->db->like('classname',$data['where']['classname']);
                unset($data['where']['classname']);
            }
            $this->db->select($data['where']);
        }
        $result = array();
        $query = $this->db->get('class');
        foreach ($query->result_array() as $row) {
            $result[$row['classid']] = $row;
        }
        return $result;
    }

    function get_class_list($data) {
        $this->db->from('class');
        if (isset($data['limit'])) {
            $this->db->limit($data['limit'], $data['offset']);
        }
        if (isset($data['where'])) {
            if(isset($data['where']['classname'])){
                $this->db->like('classname',$data['where']['classname']);
                unset($data['where']['classname']);
            }
            $this->db->where($data['where']);
        }
        if (isset($data['order'])) {
            $this->db->order_by($data['order']);
        }
        if (isset($data['column'])) {
            $this->db->select($data['column']);
        }
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_node($classid) {
        if ($classid == 0) {
            return $this->temp_node;
        }
        if (count($this->temp_node) > 5) {
            return $this->temp_node;
        }
        $this->db->select('classname,parentid,classid');
        $result = $this->db->get_where('class', array('classid' => $classid))->row_array();

        if ($result['parentid'] != 0) {
            $this->get_node($result['parentid']);
        }
        $this->temp_node[] = $result;
        return $this->temp_node;
    }

    function insert($data) {
        $this->db->insert('class', $data);
    }

    function update($data) {
        if (!isset($data['classid'])) {
            return false;
        }
        $this->db->where('classid', $data['classid']);
        unset($data['classid']);
        return $this->db->update('class', $data);
    }

    function update_by_parentid($data) {
        if (!isset($data['parentid'])) {
            return false;
        }
        $this->db->where('parentid', $data['parentid']);
        unset($data['parentid']);
        return $this->db->update('class', $data);
    }

    function delete($data) {
        return $this->db->delete('class', $data);
    }

    function get_class_tree($data) {
        $c_data = array(
            'column' => 'classid,parentid,classname',
            'where' => $data
        );
        $classlist = $this->get_class_list($c_data);
        $this->load->library('tree');
        $this->tree->setTree($classlist, 'classid', 'parentid', 'classname');
        $tree = $this->tree->getOptions(0, 0, 0, '|——');
        return $tree;
    }

}
